package com.moore.Servlet;

import com.alibaba.fastjson2.JSON;
import com.moore.net.HttpRequest;
import com.moore.net.HttpResponse;
import com.moore.net.HttpServlet;
import com.moore.utils.UserInfoUtils;

import java.net.URLDecoder;

public class UserInfoServlet extends HttpServlet {

    @Override
    public byte[] doGet(HttpRequest request, HttpResponse response) {
        String responseData = "";
        String type = request.getParams().get("type");
        String id = request.getParams().get("id");
        switch (type) {
            case "update":
                String email = URLDecoder.decode(request.getParams().get("email"));
                String phone = request.getParams().get("phone");
                String userData = UserInfoUtils.getUserInfo(id);
                if (userData.equals("fail")) {
                    responseData = UserInfoUtils.insertUserInfo(email, phone, id);
                } else {
                    responseData = UserInfoUtils.updateUserInfo(email, phone, id);
                }
                break;
            case "select":
                responseData = JSON.toJSONString(UserInfoUtils.getUserInfo(id));
                break;
        }
        return responseData.getBytes();
    }
}
